package com.lvshh.payTests;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.SecureUtil;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Locale;
import java.util.SortedMap;
import java.util.TreeMap;

import static com.lvhh.pay.utils.creatStringA;
import static com.lvhh.pay.utils.createSign;

@SpringBootTest
public class payDemo1 {

    @Test
    public void test1() throws Exception {

        SortedMap<Object, Object> parameters = new TreeMap<>();

        String api_id = "qrpay";
        String app_id = "QD010";
        String nonce_str = "be5ce85ada34466db0b55fbd6ff0e255";
        String timestamp = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss");
        // String authcode = "134648719105090423";
        // String body = "KJWY00015_鹿港大厦_城投集团_写字楼->A幢->101";
        String cusid = "m19111316042087809";
        String payway = "OF02";
        String devid = "00:1D:FA:0F:49:3E";
        String trxamt = "1";
        String version = "";
        String openid = "oZrrAvk-8l0p0qFWz-DomUKQxtPk";
        //String reqsn = "6b35614c4bf3c56040";

        Date date = DateUtil.date();
        String format = DateUtil.format(date, "yyyyMMddHHmmss");
        String randomNum = RandomUtil.randomNumbers(10);
        System.out.println(randomNum);

        String reqsn = "QD010" + format + randomNum;
        String paytype = "01";
        String paychnl = "JSAPI";
        String key = "0dSU4e9cm34J5F018fu2d9D7JVt5YR3T";
        // String notify_url = "https://www.baidu.com";
        parameters.put("api_id", api_id);
        parameters.put("app_id", app_id);
        //parameters.put("authcode", authcode);
        //parameters.put("body", body);
        parameters.put("cusid", cusid);
        parameters.put("devid", devid);
        parameters.put("nonce_str", nonce_str);
        parameters.put("payway", payway);
        // parameters.put("reqsn", reqsn);
        parameters.put("timestamp", timestamp);
        parameters.put("trxamt", trxamt);
        parameters.put("paytype", paytype);
        parameters.put("paychnl", paychnl);
        parameters.put("reqsn", reqsn);
        parameters.put("version", version);
        parameters.put("openid", openid);
        // parameters.put("notify_url", notify_url);

        String stringA = creatStringA(parameters, key);
        System.out.println("StringA:" + stringA);

        //String sign = createSign("api_id=qrpay&app_id=QD010&authcode=134648719105090423&cusid=m19111316042087809&nonce_str=be5ce85ada34466db0b55fbd6ff0e251&timestamp=2019-06-21 13:57:19&trxamt=1&key=OdSU4e9cm34J5F018fu2d9D7JVt5YR3T");
        String sign = SecureUtil.md5(stringA).toUpperCase();
        System.out.println("签名sign：" + sign);

        parameters.put("sign", sign);

        String xml = XmlUtil.mapToXmlStr(parameters);
        System.out.println(xml);

        System.out.println("==========================");
        //String sb =  new String( new XMLUtil().callMapToXML(parameters),"utf-8");

        String data = "<xml><api_id>BSC</api_id><app_id>QD002</app_id><authcode>134648719105090423</authcode>"
                + "<body>KJWY00015_鹿港大厦_城投集团_写字楼->A幢->101</body><cusid>m19061416193879083</cusid>"
                + "<devid>00:1D:FA:0F:49:3E</devid><nonce_str>be5ce85ada34466db0b55fbd6ff0e255</nonce_str>"
                + "<payway>OF02</payway><reqsn>5b35614c4bf3c558</reqsn><reserve1>KJWY00015</reserve1><sign>8762A05581D54A02248D57071AD635A1</sign>"
                + "<timestamp>2019-06-21 13:57:19</timestamp><trxamt>1</trxamt></xml>";


        //	URL url = new URL("http://www.easybots.cn/api/holiday.php?ak=k382.be7abba23cf70a1088e631b3f87ef156@dhcc.com.cn&d=20190911");
        //URL url = new URL("http://nxbk.dhjyzf.com/icmp-ums/dhc/api/service.do");
        //URL url = new URL("http://20.100.16.153:9702/icmp-ums/dhc/api/service.do");
//		URL url = new URL("http://appdemo.wzbank.com.cn:9702/icmp-ums/dhc/api/service.do");
//        URL url = new URL("http://apptest.wzbank.com.cn:9702/icmp-ums/dhc/api/service.do");
//        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
//
//        connection.setRequestMethod("POST");
//        connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
//        connection.setDoInput(true);
//        connection.setDoOutput(true);
//
//        OutputStream os = connection.getOutputStream();
//
//        os.write(xml.getBytes());
//k
//        BufferedReader reader = null;
//        StringBuffer sbf = new StringBuffer();
//        int responseCode = connection.getResponseCode();
//
//        if (responseCode == 200) {
//            InputStream in = connection.getInputStream();
//
//            //System.out.println( in.available());
//            reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
//            String strRead = null;
//            while ((strRead = reader.readLine()) != null) {
//                sbf.append(strRead);
//                sbf.append("\r\n");
//
//            }
//
//
//        }
//
//        System.out.println(sbf);
    }

}
